/*
 * Copyright 2016 <copyright holder> <email>
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */

#include "curlwrapper/chttpclient.h"
#include <stdlib.h>

#define CRLF    "\r\n"


static const char *testhttpcontent = "HTTP/1.1 302 OK\r\n"
    "Server: nginx\r\n"
    "Date: Wed, 16 Nov 2016 07:26:33 GMT\r\n"
    "Content-Type: text/html\r\n"
    "Transfer-Encoding: chunked\r\n"
    "Connection: close\r\n"
    "Access-Control-Allow-Origin: http://webagent.mail.ru\r\n"
    "Access-Control-Allow-Methods: GET, POST\r\n"
    "Access-Control-Allow-Credentials: true\r\n"
    "P3P: CP=\"NON CUR OUR IND UNI INT\"\r\n"
    "Set-Cookie: GarageID=4c4eac880190426895f61a690258c989; path=/; domain=.auth.mail.ru; Secure; HttpOnly\r\n"
    "Set-Cookie: Mpop=1479281193:58527e5146477562190502190805001b03061d05014568515c455f04000a08160302731643505e5e565040586a030407185c515f541c404d:zhongguo_200@mail.ru:; expires=Tue, 14 Feb 2017 07:26:33 GMT; path=/; domain=.mail.ru\r\n"
    "Set-Cookie: ssdc=072168ffc3d540fbbbbfdc1deeb95f64; expires=Tue, 14 Feb 2017 07:26:33 GMT; path=/; domain=.auth.mail.ru; Secure; HttpOnly\r\n"
    "Set-Cookie: ssdc_info=0721:0:1479281193; expires=Tue, 14 Feb 2017 07:26:33 GMT; path=/; domain=.auth.mail.ru; HttpOnly\r\n"
    "Set-Cookie: t=obLD1AAAAAAIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAAAAAAAAAAAAAACAAAkK0AcA; expires=Mon, 15 May 2017 07:26:33 GMT; path=/; domain=.mail.ru\r\n"
    "Location: http://r.mail.ru/clb1203896/webagent.mail.ru/r/webagent/popup.html?wa_embedlogin=636531\r\n"
    "Cache-Control: no-cache,no-store,must-revalidate\r\n"
    "Pragma: no-cache\r\n"
    "Expires: Tue, 17 Nov 2015 07:26:33 GMT\r\n"
    "Last-Modified: Wed, 16 Nov 2016 10:26:33 GMT\r\n"
    "X-Content-Type-Options: nosniff\r\n"
    "X-XSS-Protection: 1; mode=block; report=https://cspreport.mail.ru/xxssprotection\r\n"
    "Strict-Transport-Security: max-age=16070400; includeSubDomains; preload\r\n"
    "\r\n"
    "HTTP/1.1 302 Moved Temporarily\r\n"
    "Server: nginx\r\n"
    "Date: Wed, 16 Nov 2016 07:26:35 GMT\r\n"
    "Content-Type: text/html\r\n"
    "Content-Length: 154\r\n"
    "Connection: keep-alive\r\n"
    "Location: http://webagent.mail.ru/r/webagent/popup.html?wa_embedlogin=636531\r\n"
    "\r\n"
    "HTTP/1.1 200 OK\r\n"
    "Server: nginx\r\n"
    "Date: Wed, 16 Nov 2016 07:26:36 GMT\r\n"
    "Content-Type: text/html\r\n"
    "Content-Length: 466\r\n"
    "Last-Modified: Wed, 30 Sep 2015 08:17:23 GMT\r\n"
    "Connection: keep-alive\r\n"
    "ETag: \"560b9a93-1d2\"\r\n"
    "Expires: Wed, 23 Nov 2016 07:26:36 GMT\r\n"
    "Cache-Control: max-age=604800\r\n"
    "X-Frame-Options: DENY\r\n"
    "X-Content-Type-Options: nosniff\r\n"
    "X-XSS-Protection: 1; mode=block; report=https://cspreport.mail.ru/xxssprotection\r\n"
    "Accept-Ranges: bytes\r\n"
    "\r\n"
    "<!doctype html>\r\n"
    "<html>\r\n"
    "       <head>\r\n"
    "               <meta http-equiv=\"X-UA-Compatible\" content=\"IE=10\" >\r\n"
    "               <title>WebAgent popup</title>\r\n"
    "               <meta http-equiv=\"content-type\" content=\"text/html; charset =windows-1251\"/>\r\n"
    "               <script type=\"text/javascript\">\r\n"
    "                       window.__WebAgent__isPopup = true;\r\n"
    "               </script>\r\n"
    "       </head>\r\n"
    "<body>\r\n"
    "       <script type=\"text/javascript\" src=\"//img.imgsmail.ru/webim/agent/release/loader.js\"></script>\r\n"
    "\r\n"
    "</body>\r\n"
    "</html>\r\n";



void testcookieparse()
{
    Cookie_S testcook;
    for(size_t i = 0; i < 1000000; i++){
        testcook = testhttpcontent;
    }
    exit(0);
}

void setheadercallback(CURL *curl)
{
    curl_easy_setopt(curl, CURLOPT_HEADER, 1);
}

int testcurl()
{
    int ret = 0;
    CHttpClient h;
    string url = "http://www.163.com";
    string resp;
    vector<string> mHeader;
    mHeader.push_back("Accept-Encoding: gzip, deflate");
    h.setAutoDecoding("");
    h.Get(url, resp, setheadercallback, mHeader);
    return ret;
}

#ifndef NDEBUG

int main(int argc, char *argv[])
{
    return testcurl();
}
#endif